import {
	downloadOneMes2Doc,
	downloadAllMes2Doc
} from '@/pages/sys/basedoc/basedoc.js';

/**
 * @param {Object} docKey 档案key
 * @author liuwqc
 * @data 2023/09/21
 * @description 基础档案下载
 */
const downloadBaseDoc = downloadOneMes2Doc;

/**
 * @author liuwqc
 * @data 2023/09/21
 * @description 下载全部的基础档案
 */
const downloadAllBaseDoc = downloadAllMes2Doc;

/**
 * 是否是微信平台
 */
function isWeComPlatform() {
	let flag = false;
	uni.getSystemInfo({
		success(e) {
			// 通过浏览器UA,判断是否在企微浏览器中打开
			const userAgent = e.ua;
			if (userAgent.includes('wxwork') || userAgent.includes('wechat')) {
				flag = true;
			}
		}
	});
	return flag;
}

/**
 * 是否是ios平台
 */
function isIosPlatform() {
	let isIos = false;
	uni.getSystemInfo({
		success(e) {
			let platform = e.platform;
			if (platform.toLowerCase() == 'android') {
				isIos = false;
			}
			if (platform.toLowerCase() == 'ios') {
				isIos = true;
			}
		},
		complete(e) {}
	});
	return isIos;
}

/**
 * 导入企微必备jssdk
 * @param {Object} callback 回调函数
 */
function importQwJsSdk(callback) {
	const script1 = document.createElement('script');
	script1.setAttribute('type', 'text/javascript');
	script1.setAttribute('referrerpolicy', 'origin');
	script1.setAttribute('src', 'https://res.wx.qq.com/open/js/jweixin-1.2.0.js');

	script1.onload = () => {
		const script2 = document.createElement('script');
		script2.setAttribute('type', 'text/javascript');
		script2.setAttribute('referrerpolicy', 'origin');
		script2.setAttribute('src', 'https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js');

		script2.onload = () => {
			const script3 = document.createElement('script');
			script3.setAttribute('type', 'text/javascript');
			script3.setAttribute('referrerpolicy', 'origin');
			script3.setAttribute('src',
				'https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.1.1/crypto-js.min.js');

			script3.onload = () => {
				if (typeof callback === 'function' && callback !== null && callback !== undefined) {
					callback();
				}
			};
			document.head.appendChild(script3);
		};
		document.head.appendChild(script2);
	};
	script1.onerror = () => {
		console.error('js文件加载失败!');
	};
	document.head.appendChild(script1);
}

/**
 * @param {String} title 消息内容
 */
const msg = (title, duration = 1500, mask = false, icon = 'none') => {
	if (Boolean(title) === false) {
		return;
	}
	uni.showToast({
		title,
		duration,
		mask,
		icon
	});
}

/**
 * 档案id翻译成name
 * @param {String} docType 档案类型
 * @param {String} refId 档案Id
 * @param {String} valueMode 取值模式，String还是Object
 */
const getBaseDocName = (docType, refId) => {
	if (!docType || !refId) return refId
	const baseDocData = uni.getStorageSync(docType);

	let display = refId;
	if (Array.isArray(baseDocData)) {
		baseDocData.map((item) => {
			if (item.refid === refId) {
				display = item.refname;
			}
		});
	}
	return display;
}

/**
 * iso时间字符串转北京时间
 * @param {String} isoDateTime iso时间字符串
 */
const convertISODateToUTC8Date = (isoDateTime) => {
	// 创建一个新的 Date 对象
	let date = new Date(isoDateTime);
	// 将时间加上 8 小时（因为北京时间是 UTC+8）
	// date.setHours(date.getHours() + 8);
	return uni.$u.timeFormat(Number(date), 'yyyy-mm-dd hh:MM:ss');
}

import {
	formatTimestamp
} from './dateUtils.js';

const tool = {
	msg,
	isWeComPlatform,
	importQwJsSdk,
	isIosPlatform,
	downloadBaseDoc,
	downloadAllBaseDoc,
	formatTimestamp,
	getBaseDocName,
	convertISODateToUTC8Date
};

export default tool;